package com.yuanqunsoft.alimigration.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.yuanqunsoft.alimigration.bean.ReportBean;
import com.yuanqunsoft.alimigration.dao.IAlertDao;
import com.yuanqunsoft.alimigration.dao.IReportDao;
import com.yuanqunsoft.alimigration.dao.support.Page;
import com.yuanqunsoft.alimigration.model.Report;
import com.yuanqunsoft.alimigration.service.IReportService;

public class ReportServiceImpl implements IReportService {
    private IReportDao reportDao;
    private IAlertDao alertDao;

    public IReportDao getReportDao() {
        return reportDao;
    }

    public void setReportDao(IReportDao reportDao) {
        this.reportDao = reportDao;
    }

    public IAlertDao getAlertDao() {
        return alertDao;
    }

    public void setAlertDao(IAlertDao alertDao) {
        this.alertDao = alertDao;
    }

    @Override
    public ReportBean findById(int id) {
        return this.toReportBean(this.reportDao.findById(id));
    }

    @Override
    public List<?> findByApplicationId(int applicationId) {
        return this.toReportBeanList(this.reportDao.findByApplicationId(applicationId));
    }

    @Override
    public boolean addReport(Report instance) {
        try {
            this.reportDao.save(instance);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean deleteReport(int id) {
        try {
            Report instance = this.reportDao.findById(id);
            this.alertDao.deleteByReportId(id);
            this.reportDao.delete(instance);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public List<?> getAll() {
        return this.toReportBeanList(this.reportDao.findAll());
    }

    @Override
    public List<ReportBean> Query(String filters, String orders, Object... values) {
        String hql = "from Report as model";

        if (filters != null && filters != "")
            hql += " where " + filters;

        if (orders != null && orders != "")
            hql += " order by " + orders;

        return this.toReportBeanList(reportDao.hqlQuery(hql, values));
    }

    @Override
    public Page<ReportBean> pageQuery(int startNo, int pageSize, String filters, String orders, Object... values) {
        String hql = "from Report as model";

        if ((filters != null) && (!filters.isEmpty()))
            hql += " where " + filters;

        if ((orders != null) && (orders != ""))
            hql += " order by " + orders;

        return this.toReportBeanPage(reportDao.pagedQueryByStartNo(hql, startNo, pageSize, values));
    }

    @Override
    public boolean isNameExisted(String name) {
        return reportDao.isNameExisted(name);
    }

    @Override
    public boolean deleteByApplicationId(Integer appId) {
        try {
            List<Report> reportList = this.reportDao.findByApplicationId(appId);
            for(Report r : reportList){
                this.alertDao.deleteByReportId(r.getRepId());
            }
            this.reportDao.deleteByApplicationId(appId);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public ReportBean toReportBean(Report report) {
        ReportBean reportBean = new ReportBean();
        if (report.getApplication() != null) {
            reportBean.setAppId(report.getApplication().getAppId());
            reportBean.setAppName(report.getApplication().getAppName());
        }
        reportBean.setRepAdjustedEffort(report.getRepAdjustedEffort());
        reportBean.setRepAlerts(report.getRepAlerts());
        reportBean.setRepCreateTime(report.getRepCreateTime());
        reportBean.setRepEffort(report.getRepEffort());
        reportBean.setRepFileUrl(report.getRepFileUrl());
        reportBean.setRepId(report.getRepId());
        reportBean.setRepLines(report.getRepLines());
        reportBean.setRepName(report.getRepName());
        reportBean.setRepTeamAdjustment(report.getRepTeamAdjustment());
        reportBean.setRepTestFactor(report.getRepTestFactor());
        reportBean.setRepType(report.getRepType());
        reportBean.setRusId(report.getRusId());
        if (report.getTeam() != null) {
            reportBean.setTeamId(report.getTeam().getTeamId());
        }
        if(report.getTesting() !=null){
            reportBean.setTestId(report.getTesting().getTestId());
        }
        return reportBean;
    }
    public List<ReportBean> toReportBeanList(List<Report> reportList){
        List<ReportBean> reportBeanList = new ArrayList<ReportBean>();
        for(Report r : reportList){
            reportBeanList.add(this.toReportBean(r));
        }
        return reportBeanList;
    }
    
    public Page<ReportBean> toReportBeanPage(Page<Report> reportPage){
        Page<ReportBean> reportBeanPage = new Page<ReportBean>();
        reportBeanPage.setData(this.toReportBeanList(reportPage.getData()));
        reportBeanPage.setPageSize(reportPage.getPageSize());
        reportBeanPage.setStart(reportPage.getStart());
        reportBeanPage.setTotalCount(reportPage.getTotalCount());
        return reportBeanPage;
    }

}
